package middleware

import (
	"demo_bff/pkg"
	"fmt"
	"github.com/gin-gonic/gin"
	"net/http"
)

func UserAuth() gin.HandlerFunc {
	return func(c *gin.Context) {
		token := c.GetHeader("token")
		if token == "" {
			c.AbortWithStatusJSON(http.StatusUnauthorized, gin.H{
				"code": -1,
				"msg":  "token为空",
				"data": nil,
			})
			return
		}
		payload, err := pkg.GetJwtTokenPayload(token)
		if err != nil {
			c.AbortWithStatusJSON(http.StatusUnauthorized, gin.H{
				"code": -1,
				"msg":  "token解析失败",
				"data": nil,
			})
			return
		}
		fmt.Println(payload, "111111111111111111111111111111111111111111")
		c.Set("userId", payload)
		c.Next()
	}
}
